package com.gec.controller;

import com.gec.common.R;
import com.gec.entity.AddressBook;
import com.gec.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  地址管理
 * </p>
 *
 * @author : [chenxys]
 * @version : [v1.0]
 * @createTime : [2022/7/15 21:14]
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取当前用户的地址列表
     * @author chenxys
     * @return com.gec.common.R<java.util.List<com.gec.entity.AddressBook>>
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        List<AddressBook> addressBookList = addressBookService.listAll();
        log.info("获取当前用户的地址列表");
        return R.success(addressBookList);
    }

    /**
     * 获取默认地址
     * @author chenxys
     * @return com.gec.common.R<com.gec.entity.AddressBook>
     */
    @GetMapping("/default")
    public R<AddressBook> defaultAddressBook(){
        AddressBook addressBook = addressBookService.getDefaultAddressBook();
        log.info("获取默认地址{}",addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据ID获取单个地址信息
     * @author chenxys
     * @param id 地址ID
     * @return com.gec.common.R<com.gec.entity.AddressBook>
     */
    @GetMapping("/{id}")
    public R<AddressBook> getOne(@PathVariable("id") Long id){
        AddressBook addressBook = addressBookService.getOneWithId(id);
        log.info("查询单个地址信息{}",addressBook);
        return R.success(addressBook);
    }

    /**
     * 更新地址信息
     * @author chenxys
     * @param addressBook 地址信息
     * @return com.gec.common.R<java.lang.String>
     */
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateAddressBook(addressBook);
        log.info("更新地址");
        return R.success("更新地址成功");
    }

    /**
     * 保存新地址
     * @author chenxys
     * @param addressBook 地址信息
     * @return com.gec.common.R<java.lang.String>
     */
    @PostMapping
    public R<String> saveAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.saveAddressBook(addressBook);
        log.info("保存新地址");
        return R.success("保存成功");
    }

    /**
     * 设置为默认地址
     * @author chenxys
     * @param addressBook 地址信息
     * @return com.gec.common.R<java.lang.String>
     */
    @PutMapping("/default")
    public R<String> defaults(@RequestBody AddressBook addressBook){
        addressBookService.updateDefaults(addressBook);
        return R.success("更新成功");
    }
}
